package com.activity;

import android.Manifest;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;

import com.api.LoginApi;
import com.api.param.LoginParam;
import com.api.viewModel.LoginVM;
import com.baidu.idl.face.platform.FaceConfig;
import com.baidu.idl.face.platform.FaceEnvironment;
import com.baidu.idl.face.platform.FaceSDKManager;
import com.baidu.idl.face.platform.LivenessTypeEnum;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.StringUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.example.http.BuildFactory;
import com.example.lzbase.BaseActivity;
import com.example.lzbase.BaseViewModelActivity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseViewModelActivity<LoginVM> {

    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_mima)
    ImageView btnMima;
    @BindView(R.id.btn_denglu)
    Button btnDenglu;
    @BindView(R.id.et_phone)
    EditText etPhone;
    private boolean isFace;


    @OnClick(R.id.btn_mima)
    public void onClick() {

        btnMima.setSelected(!btnMima.isSelected());

        if (btnMima.isSelected()) {
            etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            etPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }

    }

    @Override
    protected int titleText() {
        return R.string.app_name;
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        super.onPermissionsGranted(requestCode, perms);

        if(requestCode==1000){

            if(isFace){
                ActivityUtil.startActivity(LoginActivity.this,FaceLivenessExpActivity.class);

            }
        }
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {

        super.created(savedInstanceState);


        viewModel.user.observe(this,res->{


            res.setPwd(etPwd.getText().toString());
            App.getInstance().setAccount(res);
            ActivityUtil.startMainActivity(this);


        });
        if(App.getInstance().getAccount()!=null){
            etPhone.setText(App.getInstance().getAccount().getUsername());
            etPwd.setText(App.getInstance().getAccount().getPwd());
        }




    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login2;
    }


    @OnClick({R.id.btn_register, R.id.btn_forget_pwd, R.id.btn_denglu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register:

                ActivityUtil.startActivity(this, RegisterActivity.class);
                break;
            case R.id.btn_forget_pwd:
                ActivityUtil.startActivity(this, ForgetPwdActivity.class);
                break;
            case R.id.btn_denglu: {
                if (etPhone.getText().toString().trim().length() == 0) {

                    ToastUtil.toast(this, R.string.qing_shu_ru_shou_ji_hao);
                    return;
                }
                if (!StringUtil.isMobileNO(etPhone.getText().toString().trim())) {

                    ToastUtil.toast(this, R.string.qing_shu_ru_zheng_que_shou_ji_hao);
                    return;
                }
                if (etPwd.getText().toString().trim().length() == 0) {

                    ToastUtil.toast(this, R.string.qing_shu_ru_mi_ma);
                    return;
                }
                addSubscription(viewModel.login(new LoginParam(etPhone.getText().toString().trim(),etPwd.getText().toString())));
            }

            break;
        }
    }

    @Override
    public int statusColor() {
        return R.color.white;
    }



}
